#include "IIC.h"
#include "delay.h"
#include "stm32f10x_i2c.h"
#include "usart.h"


#define BYTE_SWAP(b) (( b >> 4) | ((b & 0x0F) << 4) )

const uint8_t bitrev_lut[16] = { 0x0, 0x8, 0x4, 0xc, 0x2, 0xa, 0x6, 0xe,
      0x1, 0x9, 0x5, 0xd, 0x3, 0xb, 0x7, 0xf };
  



//********�ӿڶ���*********************************************************//
//���ÿ⺯���� I2C1�˿����úã�����ʹ�õ��� PB6��PB6 �˿ڣ���
//************************************************************************//

void R820T_IIC_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;                                    //GPIO �ṹ�嶨��
    I2C_InitTypeDef I2C_InitStructure;                                      //I2C �ṹ�嶨��
	
	 //*I2C1-IO ������*//
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);                     //ʹ��GPIOBʱ��
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;                           //���ù��ܵĿ�©���
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;                    //ʹ��PB6��PB7
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                         //�ٶ����50MHz
    GPIO_Init(GPIOB, &GPIO_InitStructure);                                    //��ʼ���ṹ������
	GPIO_PinRemapConfig(GPIO_Remap_I2C1, ENABLE);
	
    //*I2C1-ģʽ ����*//
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);	                //I2C1 ʱ��ʹ��
    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;                              //ѡ��I2C����
    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;                             //I2CӦ��ʹ��
    I2C_InitStructure.I2C_ClockSpeed = 400000;                              //ʱ�����ʣ��� HZ Ϊ��λ�ģ����Ϊ 400khz
    I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;                      //�ò���ֻ���� I2C �����ڿ���ģʽ��ʱ�ӹ���Ƶ�ʸ��� 100KHz���²�������
    I2C_InitStructure.I2C_OwnAddress1 = 0xFF;                               //���õ�һ���豸�����ַ
    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; //MPU9250 ��ַΪ 7 λ�������� 7 λ������
    I2C_Init(I2C1, &I2C_InitStructure);                                      //��ʼ���ṹ������
    I2C_Cmd(I2C1, ENABLE);                                                   //ʹ�� I2C1

}


//************IIC���ֽ�д����******************************************//
u8 R820T_Write_Byte(u8 ADDR, u8 reg, u8 len, uint8_t *buf)
{
    delay_ms(2);
    I2C_GenerateSTART(I2C1, ENABLE);                                                  //������ʼλ
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));                       //��� EV5
    I2C_Send7bitAddress(I2C1, ADDR, I2C_Direction_Transmitter);                       //����������ַ
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));         //ADDR=1����� EV6
    I2C_SendData(I2C1, reg);                                                          //�Ĵ��������ַ
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));                  //��λ�Ĵ����ǿգ����ݼĴ����Ѿ��գ����� EV8���������ݵ� DR �ȿ�������¼�
    while(len--)                                                                      //���� while ѭ�� ��������
    {
        I2C_SendData(I2C1, *buf);                                                     //��������
        buf++;                                                                        //����ָ����λ
        while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));             //���EV8
    }
    I2C_GenerateSTOP(I2C1, ENABLE);                                                   //����ֹͣ�ź�
    return 0;
}


//***********IIC���ֽڶ�����********************************************//
u8 R820T_Read_Byte(u8 ADDR, u8 reg, u8 len, u8 *buf)                             //��Ҫ������ʼ�ź�
{
	uint8_t value;
    //delay_ms(2);
    while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));                                    //���ÿ⺯����� I2C �����Ƿ��� BUSY ״̬
    I2C_GenerateSTART(I2C1, ENABLE);                                                  //�����ź�
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));                       //��� EV5
    I2C_Send7bitAddress(I2C1, ADDR, I2C_Direction_Transmitter);                       //д��������ַ
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));         //��� EV6
    I2C_SendData(I2C1, reg);                                                          //���Ͷ��ĵ�ַ
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));                  //��� EV8
    I2C_GenerateSTART(I2C1, ENABLE);                                                  //�����ź�
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));                       //��� EV5
    I2C_Send7bitAddress(I2C1, ADDR, I2C_Direction_Receiver);                          //��������ַ����������Ϊ��
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));            //���EV6
    while(len)
    {
        if(len == 1)//ֻʣ�����һ������ʱ���� if ���
        {
            I2C_AcknowledgeConfig(I2C1, DISABLE);//�����һ������ʱ�ر�Ӧ��λ
            I2C_GenerateSTOP(I2C1, ENABLE);//���һ������ʱʹ��ֹͣλ
        }
        if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) //��ȡ����
        {
            value = I2C_ReceiveData(I2C1);//���ÿ⺯��������ȡ���� pBuffer
			/* bit reverse into destination */
			*buf = (bitrev_lut[value & 0xf] << 4) | (bitrev_lut[value >> 4]);
            buf++; //ָ����λ
            len--;//�ֽ����� 1
        }
    }
    I2C_AcknowledgeConfig(I2C1, ENABLE);                                              //��Ӧ��λʹ�ܻ�ȥ���ȴ��´�ͨ��
    return 0;
}



